<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\exam\logic\admin\QuestionPoint as QuestionPointLogic;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 知识点-后台
 */
class QuestionPoint extends BaseService
{
    /**
     * @Inject()
     *
     * @var QuestionPointLogic
     */
    protected $questionPointLogic;

    /**
     * 知识点列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/6
     *
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $data = $this->questionPointLogic->getList();
        // 定义字段
        $keyList = [
            'title'        => ListItem::make()->title('知识点名称'),
            'category_str' => ListItem::make()->title('知识点分类'),
            'num'          => ListItem::make()->title('题目数量'),
        ];

        // 设置表单
        $list = new PageList;
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 获取知识点列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/6
     *
     * @param $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChildren(int $id)
    {
        $data = $this->questionPointLogic->getList($id);

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * 创建知识点
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/6
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Request::post();
        $re   = $this->questionPointLogic->createPoint($data);

        return $this->parseToData($re, 1, '创建成功');
    }

    /**
     * 编辑知识点
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/6
     *
     * @param $id
     * @return \think\Response
     */
    public function edit(int $id)
    {
        $data = Request::put();
        $re   = $this->questionPointLogic->editPoint($id, $data);

        return $this->parseToData($re, 1, '编辑成功');
    }

    /**
     * 删除知识点
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/6
     *
     * @param $id
     * @return \think\Response
     */
    public function delete(int $id)
    {
        $this->questionPointLogic->deletetPoint($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 知识点选择面板
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/23
     *
     * @return \think\Response
     */
    public function choose()
    {
        $map = [];
        $cates = Request::get('cates/s', '');
        $cates && $map[] = ['category', '=', $cates];
        // 知识点分类
        $category = $this->questionPointLogic->getTopList($map);

        return $this->parseToData(
            [
                'category' => $category,
            ],
            1
        );
    }

    /**
     * 获取知识点选择面板列表数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/11/23
     *
     * @return \eduline\admin\page\interfaces\think\Response
     */
    public function getChooseList()
    {
        $map = [];
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $category = Request::get('category/d', 0);
        $category && $map[] = ['pid', '=', $category];

        $cates = Request::get('cates/s', '');
        $cates && $map[] = ['category', '=', $cates];

        $datas = $this->questionPointLogic->getPageList($map);

        $list        = new PageList();
        $list->datas = $datas;

        return $list->send();
    }
}
